package com.hidtechs.popfax.xml;
import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;




public class XMLLoginHandler extends DefaultHandler{ 

	// =========================================================== 
	// Fields 
	// =========================================================== 

	private boolean in_outertag = false; 
	private boolean in_innertag = false; 
	private boolean in_mytagError = false;
	private boolean in_mytagResult = false;
	private ParsedExampleDataSet obj;
	public ArrayList<Object> arrayOfObject;
	
	//private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();
	
	@Override 
	public void startDocument() throws SAXException { 
		arrayOfObject = new ArrayList<Object>(); 
		System.out.println("XMLLOgin start Doc="+arrayOfObject);
	//	this.myParsedExampleDataSet = new ParsedExampleDataSet();
	} 
	
	
//	public ParsedExampleDataSet getParsedData() { 
//		return this.myParsedExampleDataSet; 
//	}

	@Override 
	public void endDocument() throws SAXException { 
		// Nothing to do 
	} 

	/** Gets be called on opening tags like: 
	 * <tag> 
	 * Can provide attribute(s), when xml was like: 
	 * <tag attribute="attributeValue">*/ 
	@Override 
	public void startElement(String namespaceURI, String localName, 
			String qName, Attributes atts) throws SAXException { 
		if (localName.equals("API_RESULT")) { 
			
			obj = new ParsedExampleDataSet();
			this.in_outertag = true; 
		}else if (localName.equals("AUTHENTICATION")) {
			
			this.in_innertag = true; 
		}else if (localName.equals("RESULT") ) { 
			
			this.in_mytagResult = true; 
		}	else if (localName.equals("ERROR") ) { 
			
			this.in_mytagError = true; 
		}
	}
	/** Gets be called on closing tags like: 
	 * </tag> */ 
	@Override 
	public void endElement(String namespaceURI, String localName, String qName) 
	throws SAXException { 
		if (localName.equals("API_RESULT")) { 
			
			arrayOfObject.add(0,obj);
			this.in_outertag = false; 
		}else if (localName.equals("AUTHENTICATION")) {
			
			this.in_innertag = false; 
		}else if (localName.equals("RESULT") ) { 
			
			this.in_mytagResult = false;      
		}
		else if (localName.equals("ERROR") ) { 
			
			this.in_mytagError = false; 
		}
	} 

	/** Gets be called on the following structure: 
	 * <tag>characters</tag> */ 
	@Override 
	public void characters(char ch[], int start, int length) { 
		if(this.in_mytagResult){ 
			
			obj.setResult(Integer.parseInt((new String(ch, start, length)))); 
		}
		else if (this.in_mytagError){ 
			
			obj.setResult(Integer.parseInt((new String(ch, start, length)))); 
		}
	} 
}

